package com.michaelbulava.TFS.ui.controls.browser;

import com.intellij.icons.AllIcons;
import com.intellij.ide.IdeBundle;
import com.intellij.ide.browsers.BrowserFamily;
import com.intellij.ide.browsers.BrowserSpecificSettings;
import com.intellij.ide.browsers.WebBrowser;
import com.intellij.ide.browsers.WebBrowserManager;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.PathUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.UUID;

/**
 * Created by mbulava on 6/29/2015.
 */
public class ConfigurableWebBrowser extends WebBrowser {
    private final UUID id;
    @NotNull
    private BrowserFamily family;
    @NotNull
    private String name;
    private boolean active;
    private String path;
    private BrowserSpecificSettings specificSettings;

    public ConfigurableWebBrowser() {
        this(UUID.randomUUID(), BrowserFamily.CHROME);
    }

    public ConfigurableWebBrowser(@NotNull UUID id, @NotNull BrowserFamily family) {
        this(id, family, family.getName(), family.getExecutionPath(), true, family.createBrowserSpecificSettings());
    }

    public ConfigurableWebBrowser(@NotNull UUID id, @NotNull BrowserFamily family, @NotNull String name, @Nullable String path, boolean active, @Nullable BrowserSpecificSettings specificSettings) {
        this.id = id;
        this.family = family;
        this.name = name;
        this.path = StringUtil.nullize(path);
        this.active = active;
        this.specificSettings = specificSettings;
    }

    public void setName(@NotNull String value) {
        this.name = value;
    }

    public void setFamily(@NotNull BrowserFamily value) {
        this.family = value;
    }

    @NotNull
    public Icon getIcon() {
        if(this.family == BrowserFamily.CHROME) {
            Icon var10000;
            if(WebBrowserManager.isYandexBrowser(this)) {
                var10000 = AllIcons.Xml.Browsers.Yandex16;
                if(AllIcons.Xml.Browsers.Yandex16 == null) {
                    throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[]{"com/intellij/ide/browsers/ConfigurableWebBrowser", "getIcon"}));
                }

                return var10000;
            }

            if(this.checkNameAndPath("Dartium") || this.checkNameAndPath("Chromium")) {
                var10000 = AllIcons.Xml.Browsers.Chromium16;
                if(AllIcons.Xml.Browsers.Chromium16 == null) {
                    throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[]{"com/intellij/ide/browsers/ConfigurableWebBrowser", "getIcon"}));
                }

                return var10000;
            }

            if(this.checkNameAndPath("Canary")) {
                var10000 = AllIcons.Xml.Browsers.Canary16;
                if(AllIcons.Xml.Browsers.Canary16 == null) {
                    throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[]{"com/intellij/ide/browsers/ConfigurableWebBrowser", "getIcon"}));
                } else {
                    return var10000;
                }
            }

            if(this.checkNameAndPath("Opera")) {
                var10000 = AllIcons.Xml.Browsers.Opera16;
                if(AllIcons.Xml.Browsers.Opera16 == null) {
                    throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[]{"com/intellij/ide/browsers/ConfigurableWebBrowser", "getIcon"}));
                }

                return var10000;
            }

            if(this.checkNameAndPath("node-webkit") || this.checkNameAndPath("nw") || this.checkNameAndPath("nwjs")) {
                var10000 = AllIcons.Xml.Browsers.Nwjs16;
                if(AllIcons.Xml.Browsers.Nwjs16 == null) {
                    throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[]{"com/intellij/ide/browsers/ConfigurableWebBrowser", "getIcon"}));
                }

                return var10000;
            }
        }

        return this.family.getIcon();
    }

    private boolean checkNameAndPath(@NotNull String what) {
        //return WebBrowserManager.checkNameAndPath(what, this);
        if(StringUtil.containsIgnoreCase(this.getName(), what)) {
            return true;
        } else {
            String path = this.getPath();
            if(path != null) {
                int index = path.lastIndexOf(47);
                return index > 0?path.indexOf(what, index + 1) != -1:path.contains(what);
            } else {
                return false;
            }
        }
    }

    @Nullable
    public String getPath() {
        return this.path;
    }

    public void setPath(@Nullable String value) {
        this.path = PathUtil.toSystemIndependentName(StringUtil.nullize(value));
    }

    @Nullable
    public BrowserSpecificSettings getSpecificSettings() {
        return this.specificSettings;
    }

    public void setSpecificSettings(@Nullable BrowserSpecificSettings value) {
        this.specificSettings = value;
    }

    public boolean isActive() {
        return this.active;
    }

    public void setActive(boolean value) {
        this.active = value;
    }

    public boolean equals(Object o) {
        if(this == o) {
            return true;
        } else if(!(o instanceof ConfigurableWebBrowser)) {
            return false;
        } else {
            ConfigurableWebBrowser browser = (ConfigurableWebBrowser)o;
            return this.getId().equals(browser.getId()) && this.family.equals(browser.family) && this.active == browser.active && Comparing.strEqual(this.name, browser.name) && Comparing.equal(this.path, browser.path) && Comparing.equal(this.specificSettings, browser.specificSettings);
        }
    }

    public int hashCode() {
        return this.getId().hashCode();
    }

    @NotNull
    public String getName() {
        String var10000 = this.name;
        if(this.name == null) {
            throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[]{"com/intellij/ide/browsers/ConfigurableWebBrowser", "getName"}));
        } else {
            return var10000;
        }
    }

    @NotNull
    public final UUID getId() {
        UUID var10000 = this.id;
        if(this.id == null) {
            throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[]{"com/intellij/ide/browsers/ConfigurableWebBrowser", "getId"}));
        } else {
            return var10000;
        }
    }

    @NotNull
    public BrowserFamily getFamily() {
        BrowserFamily var10000 = this.family;
        if(this.family == null) {
            throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[]{"com/intellij/ide/browsers/ConfigurableWebBrowser", "getFamily"}));
        } else {
            return var10000;
        }
    }

    @NotNull
    public String getBrowserNotFoundMessage() {
        return IdeBundle.message("error.0.browser.path.not.specified", new Object[]{this.getName()});
    }

    public String toString() {
        return this.getName() + " (" + this.getPath() + ")";
    }
}
